package com.cyy.dao;
import com.cyy.bean.product;
import com.cyy.util.dbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class productDao {
    public ArrayList<product> selectAll() throws SQLException, ClassNotFoundException {
//        获得所有未下架的商品
        Connection conn = dbUtil.getConnection();
        String sql="select * from product where usability=1";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet resultSet = pstmt.executeQuery();
        String title="";
        String cover="";
        int price=0,reserve=0,sold=0,id=0;
        product data=null;
        ArrayList<product> datas=new ArrayList<>();
        while(resultSet.next())
        {
            id=resultSet.getInt("id");
            cover=resultSet.getString("cover");
            title=resultSet.getString("name");
            price=resultSet.getInt("price");
            reserve=resultSet.getInt("reserve");
            sold=resultSet.getInt("sold");
            data=new product(id,title,price,reserve,sold,cover);
            datas.add(data);
        }
        return datas;
    }

    public boolean addProduct(String[] names,int[] prices,int[] reserves,int[] solds,String[] covers) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="insert into product(name, price, reserve, sold, cover) value (?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        int k=0;
        for(int i=0;i<prices.length;i++) {
            pstmt.setString(1, names[i]);
            pstmt.setDouble(2,prices[i]);
            pstmt.setInt(3,reserves[i]);
            pstmt.setInt(4,solds[i]);
            pstmt.setString(5,covers[i]);
            k = pstmt.executeUpdate();
        }
        System.out.println("插入完成！");
        dbUtil.close(null,pstmt,conn);
        if(k!=0)
        {
            return true;
        }
        return false;
    }

    public void updateProduct(String[] infos) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="update product set price=?,reserve=? where id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        String temp=infos[2].replaceAll("[^0-9]", "");
        pstmt.setInt(1, Integer.parseInt(infos[1]));
        pstmt.setInt(2, Integer.parseInt(temp));
        pstmt.setInt(3, Integer.parseInt(infos[0]));
        int i = pstmt.executeUpdate();
        dbUtil.close(null,pstmt,conn);
    }

    public void deleteById(int id) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="update product set usability=0 where id=?";
        String sql1="update record set usability=0 where productName =(select name from product where id=?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        PreparedStatement pstmt1 = conn.prepareStatement(sql1);
        pstmt.setInt(1,id);
        pstmt1.setInt(1,id);
        int i = pstmt.executeUpdate();
        int i1 = pstmt1.executeUpdate();
    }

    public int selectUsabilityByName(String productName) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="select usability from product where name=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,productName);
        ResultSet resultSet = pstmt.executeQuery();
        int usability=0;
        if(resultSet.next())
        {
            usability=resultSet.getInt("usability");
        }
        return usability;
    }
    public int getMaxid() throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="select * from product";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet resultSet = pstmt.executeQuery();
        int num=0;
        if(resultSet.next())
        {
            num+=1;
        }
        return num;
    }
}
